package org.itboys.admin.service.lasted;

import org.itboys.admin.dao.SundayAreaMapper;
import org.itboys.admin.dto.lasted.SundayAreaDto;
import org.itboys.admin.entity.lasted.SundayArea;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mongodb.service.BaseMySqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 包名：com.hz.sunday.tianjie.entity.mysql.product
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2016年3月10日 下午2:52:46
 * 描述: 圣代_省（直辖市）/市/区or县/街道or乡镇_service。四级行政区划
 *
 */


@Service
public class SundayAreaService {
    /**
     * 项目启动时缓存省（直辖市）/市/区or县/街道or乡镇数据
     */
    public static List<SundayArea> all_provinces;
    public static List<SundayArea> all_citys;
    public static List<SundayArea> all_districts;
    public static List<SundayArea> all_towns;

    @Autowired
    private SundayAreaMapper areaMapper;
    //获取树结构省市区。

    /**
     * 获取省（直辖市）/市/区or县/街道or乡镇。树结构类型数据
     * @param isIncludeTown。是否包含乡镇
     * @return
     */
    public List<SundayArea> getAreaTree(boolean isIncludeTown){
        Map<String,Object> param = new HashMap<String,Object>();
        for(SundayArea province:all_provinces){
            List<SundayArea> citys = new ArrayList<SundayArea>();
            for(SundayArea city:all_citys){
                //匹配市
                if(city.getParentId().longValue()==province.getId().longValue()){
                    List<SundayArea> districts = new ArrayList<SundayArea>();
                    for(SundayArea district:all_districts){
                        //匹配区县
                        if(district.getParentId().longValue()==city.getId().longValue()){

                            List<SundayArea> towns = new ArrayList<SundayArea>();
                            if(isIncludeTown){
                                for(SundayArea town:all_towns){
                                    //匹配街道乡镇
                                    if(town.getParentId().longValue()==district.getId().longValue()){
                                        towns.add(town);
                                    }
                                }
                                district.setChildren(towns);
                            }
                            districts.add(district);
                        }


                    }
                    city.setChildren(districts);
                    citys.add(city);
                }
            }
            province.setChildren(citys);

        }
        return all_provinces;
    }



    /**
     *
     *2016年12月20日-------------------静态获取
     */
    /**
     * 获取省。启动时已缓存
     * @return
     */
    public  List<SundayArea> getProvince(){
        return  all_provinces;
    }

    /**
     * 获取市。启动时已缓存
     * @param provinceId
     * @return
     */
    public  List<SundayArea> getCity(Long provinceId){

        List<SundayArea> citys = new ArrayList<SundayArea>();
        for(SundayArea city:all_citys){
            if(city.getParentId().longValue()==provinceId.longValue()){
                citys.add(city);
            }
        }
        return citys;
    }
    /**
     * 获取区县。启动时已缓存
     * @param cityId
     * @return
     */
    public  List<SundayArea> getDistrict(Long cityId){
        List<SundayArea> districts = new ArrayList<SundayArea>();
        for(SundayArea district:all_districts){
            if(district.getParentId().longValue()==cityId.longValue()){
                districts.add(district);
            }
        }
        return districts;
    }

    /**
     * 获取街道乡镇
     * @param districtId
     * @return
     */
    //获取街道乡镇
    public  List<SundayArea> getTown(Long districtId){
        List<SundayArea> towns = new ArrayList<SundayArea>();
        for(SundayArea town:all_towns){
            if(town.getParentId().longValue()==districtId.longValue()){
                towns.add(town);
            }
        }
        return towns;
    }


}
